﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using moose.irc;

namespace mooseirc.controls
{
	/// <summary>
	/// Used to track Users in channel
	/// </summary>
	/// <remarks>
	/// Creating this control, kinda messy for now, 
	/// though don't really care, for now..
	/// </remarks>
	public partial class NickListControl 
		: UserControl
	{
		#region [Local Attributes]
		
		/// <summary>
		/// Collection of users represented in this
		/// </summary>
		private List<IrcUser> m_users;
		
		/// <summary>
		/// Opped Users
		/// </summary>
		private List<IrcUser> m_op;
		
		//Voiced Users
		private List<IrcUser> m_voice;
		
		#endregion
		
		#region [Construction]
		
		/// <summary>
		/// Initialize Nick List
		/// </summary>
		public NickListControl()
			: base()
		{
			InitializeComponent();
			
			m_users = new List<IrcUser>();
			m_op = new List<IrcUser>();
			m_voice = new List<IrcUser>();
			
			return;
		}
		
		#endregion
		
		#region [Public Methods]
		
		/// <summary>
		/// Update User List
		/// </summary>
		/// <param name="channel">Channel to source all users from</param>
		public void update_userlist(IrcChannel channel) {
			//Clear collections first
			m_users.Clear();
			m_op.Clear();
			//m_voice();
			
			//Next rebuild collections
			
			
			
			return;
		}
		
//		/// <summary>
//		/// Add a user to the list
//		/// </summary>
//		/// <param name="user">user to add</param>
//		public void add(IrcUser user) {
//			if (!m_users.Contains(user))
//				m_users.Add(user);
//			Invalidate();
//			return;
//		}
//		
//		/// <summary>
//		/// Remove User from list
//		/// </summary>
//		/// <param name="user">user to remove</param>
//		public void remove(IrcUser user) {
//			if (m_users.Contains(user))
//				m_users.Remove(user);
//			Invalidate();
//			return;
//		}
		
		#endregion
		
		#region [Overrides]
		
		/// <summary>
		/// Fires when painting
		/// </summary>
		/// <param name="e">Paint Event Args</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			SortedList<int, string> users_print = new SortedList<int, string>();
			int op = 0;
			int voice = 1000;
			int reg = 2000;
			//Organize
			foreach (IrcUser user in m_users) {
				if (m_op.Contains(user)) {
					users_print.Add(op++, "@" + user.Nickname);
				} else if (m_voice.Contains(user)) {
					users_print.Add(voice++, "+" + user.Nickname);
				} else {
					users_print.Add(reg++, "+" + user.Nickname);
				}
			}
			
			int x = 2,
                y = 0;
			using (Bitmap graphics_buffer = new Bitmap(this.Width, this.Height, e.Graphics))
            {
                using (Graphics g = Graphics.FromImage(graphics_buffer))
                {
                	g.Clear(BackColor);
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    
                    foreach (string nickname in users_print.Values) {
                    	g.DrawString(nickname,
                            this.Font,
                            new SolidBrush(this.ForeColor),
                            x, y, StringFormat.GenericTypographic);
                        y += Font.Height + 2;
                    }
                    
                    e.Graphics.DrawImageUnscaled(
					graphics_buffer, 0, 0);
                }
			}    
			return;
		}
		
		/// <summary>
		/// Fires on windows message
		/// </summary>
		/// <param name="m"></param>
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg) {
				case Win32.WM_ERASEBKGND:
					break;
				default:		
				base.WndProc(ref m);
				break;
			};

		}
		
		#endregion
		
	}
}
